﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using RbacPermissionManage.Application.Admins.Dto;
using RbacPermissionManage.Common;
using RbacPermissionManage.DoMain;
using RbacPermissionManage.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RbacPermissionManage.Application.Admins
{
    public class AdminServices : IAdminServices
    {
        private readonly IRepository<Admin> repository;
        private readonly IMapper mapper;

        public AdminServices(IRepository<Admin> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="registerDto"></param>
        /// <returns></returns>
        public async Task<ResultDto> Register(RegisterDto registerDto)
        {
            //查用户名
            var admin = await repository.FirstOrDefaultAsync(x => x.UserName == registerDto.UserName);
            if (admin != null)
            {
                return new ResultDto { Code = Code.Failed, Message = "用户名已使用" };
            }

            //MD5加密
            //registerDto.Pwd.Md5();

            //添加数据
            Admin adminInfo = new Admin
            {
                UserName = registerDto.UserName,
                Pwd = registerDto.Pwd.Md5(),//Md5加密
                CreateName = "张三",
                CreateTime = DateTime.Now,
            };

            try
            {
                await repository.InsertAsync(adminInfo);//调仓储的添加方法
                return new ResultDto { Code = Code.Success };
            }
            catch (Exception e)
            {
                return new ResultDto { Code = Code.Failed, Message = e.Message };
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        public async Task<LoginResultDto> LoginAsync(LoginDto loginDto)
        {
            var admin = await repository.FirstOrDefaultAsync(x => x.UserName == loginDto.UserName);
            if(admin == null)
            {
                return new LoginResultDto { Code=LoginEnum.NoAccount,Message="用户名不正确"};
            }
            else
            {
                if (admin.Pwd != loginDto.Pwd.Md5())
                {
                    return new LoginResultDto { Code = LoginEnum.PasswordError, Message="密码错误"};
                }
                else
                {
                    return new LoginResultDto { Code = LoginEnum.Success };
                }
            }
        }

        /// <summary>
        /// 查询分页显示管理员列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<ResultDto<(int,List<AdminListDto>)>> GetAdminAsync(AdminSearchDto searchDto,int pageIndex,int pageSize)
        {
            try
            {
                var list = repository.Queryable().Where(x => x.IsDel == false);
                if (!string.IsNullOrEmpty(searchDto.Keywords))
                {
                    list = list.Where(x => x.UserName.Contains(searchDto.Keywords) || x.Email.Contains(searchDto.Keywords));
                }
                if (searchDto.StartTime != null)
                {
                    list=list.Where(x=>x.CreateTime>=searchDto.StartTime);
                }
                if(searchDto.EndTime!= null)
                {
                    list=list.Where(x=>x.CreateTime<=searchDto.EndTime);
                }
                var PageData = await list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
                var result = mapper.Map<List<Admin>, List<AdminListDto>>(PageData);

                //var result = mapper.Map<List<Admin>, List<AdminListDto>>(list);
                return new ResultDto<(int,List<AdminListDto>)> { Data=(await list.CountAsync(),result) };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
